#include #include #include #include using namespace std; bool lineContainsWord(string line, string word) { bool result = false; unsigned int indexOfWord = line.find(word); if(indexOfWord != string::npos) { //check begining or word if(indexOfWord == 0 || !isalpha(line[indexOfWord -1])) { int lengthOfWord = word.length(); int lengthOfLine = line.length(); //check begining or word if(indexOfWord + lengthOfWord == lengthOfLine || !isalpha(line[indexOfWord + lengthOfWord])) { result = true; } } } return result; } void main() { ifstream bibleIn("bible.txt"); ofstream bibleOut("love.txt"); //bibleIn.open("bible.txt"); string line; string word; cout << "Word to search for: "; cin >> word; //eof end of file, the char at the end of a file //file handle - variable used to work with files while(!bibleIn.eof()) { getline(bibleIn,line); if(line.find("Book") != string::npos || lineContainsWord(line,word)) { bibleOut << line << endl; } } bibleIn.close(); bibleOut.close(); }